package com.yxhl.zoume.core.user.ui.fragment.mywallet;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.afollestad.materialdialogs.MaterialDialog;
import com.android.databinding.library.baseAdapters.BR;
import com.yxhl.zoume.R;
import com.yxhl.zoume.account.event.LoginStatus;
import com.yxhl.zoume.account.event.LoginViewBackEvent;
import com.yxhl.zoume.common.ui.fragment.BaseDataBindingFragment;
import com.yxhl.zoume.core.func.webpage.info.WebPageEntrance;
import com.yxhl.zoume.core.user.presenter.mywallet.WalletBalancePresenter;
import com.yxhl.zoume.core.user.ui.activity.MyWalletActivity;
import com.yxhl.zoume.core.user.ui.info.MyWalletEntry;
import com.yxhl.zoume.core.user.view.IWalletBalanceView;
import com.yxhl.zoume.data.http.model.mywallet.WalletBalanceBean;
import com.yxhl.zoume.di.component.usercenter.DaggerMyWalletComponent;
import com.yxhl.zoume.di.module.ActivityModule;
import com.yxhl.zoume.utils.PromptUtils;

import javax.inject.Inject;

import butterknife.OnClick;
import rx.functions.Action1;


/**
 * 项目名称：Ferrari
 * Created by menggod on 16/11/3 .
 */

public class WalletHomeFragment extends BaseDataBindingFragment implements IWalletBalanceView {


    private ViewDataBinding mDataBinding;
    MaterialDialog dialogForWaitingDetail = null;
    @Inject
    WalletBalancePresenter mWalletBalancePresenter;

    public static WalletHomeFragment newInstance() {
        WalletHomeFragment fragment = new WalletHomeFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onRxbusEvent();
    }

    private void onRxbusEvent() {
        getSubscription().add(toSubscription(LoginViewBackEvent.class, new Action1<LoginViewBackEvent>() {
            @Override
            public void call(LoginViewBackEvent loginViewBackEvent) {
                LoginStatus isLogin = loginViewBackEvent.getIsLogin();
                if (isLogin == LoginStatus.NOT_LOGIN){
                    MyWalletActivity activity = (MyWalletActivity) getActivity();
                    activity.finish();
                }
            }
        }));
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mWalletBalancePresenter.attachView(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        mWalletBalancePresenter.queryWalletBalance();
        showWaitingDialog(true);
    }

    @Override
    public void onResume() {
        super.onResume();
        mWalletBalancePresenter.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mWalletBalancePresenter.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mWalletBalancePresenter.onDestroy();
    }


    @Override
    protected void initializeInjector() {
        DaggerMyWalletComponent
                .builder()
                .activityModule(new ActivityModule(mActivity))
                .appComponent(getAppComponent())
                .build()
                .inject(this);

    }

    @Override
    protected View getContentLayout(LayoutInflater inflater, ViewGroup container) {
        mDataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_my_wallet_home, container, false);
        return mDataBinding.getRoot();
    }


    @Override
    protected void setupToolBar() {
        ((MyWalletActivity) mActivity).setToolbarTitle("我的钱包");
    }


    @OnClick({R.id.btn_wallet_recharge, R.id.btn_wallet_inex_detail,R.id.ll_wallet_inex_problem})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_wallet_recharge:
                ((MyWalletActivity) mActivity).showRelevantFragment(MyWalletEntry.RECHARGE);
                break;
            case R.id.btn_wallet_inex_detail:
                ((MyWalletActivity) mActivity).showRelevantFragment(MyWalletEntry.INCOME_EXPENSES);
                break;
            case R.id.ll_wallet_inex_problem:
                getAppComponent().navigator().navigateToWebPageActivity(mActivity, WebPageEntrance.WALLET_INTRODUCTIONS);
                break;
        }
    }

    @Override
    public void renderWalletBalanceOnSuccess(WalletBalanceBean walletBalanceBean) {
        showWaitingDialog(false);
        mDataBinding.setVariable(BR.walletBalanceBean, walletBalanceBean);
    }

    @Override
    public void renderWalletBalanceOnFailure(String errorMessage) {
        showWaitingDialog(false);
//        PromptUtils.showShort(mActivity, errorMessage);
//            if (errorMessage.equals("请先登录")) {
//                MyWalletActivity activity = (MyWalletActivity) getActivity();
//                activity.finish();
//        }
    }

    @Override
    public void renderWalletBalanceOnError(Throwable e) {
        showWaitingDialog(false);
        PromptUtils.showShort(mActivity, e.getMessage());
    }

    public void showWaitingDialog(boolean isWaiting) {
        if (isWaiting) {
            dialogForWaitingDetail = new MaterialDialog.Builder(mActivity)
                    .content(R.string.dialog_waiting_processing)
                    .progress(true, 0)
                    .canceledOnTouchOutside(false)
                    .show();
        } else {
            if (null != dialogForWaitingDetail) {
                dialogForWaitingDetail.dismiss();
            }
        }

    }
}
